﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Rehabilitation.Models
{
    public class Tools
    {
        public static bool IsAdmin()
        {
            User user = (User)HttpContext.Current.Session["User"];
            if (user == null) return false;
            using (DBEntities db = new DBEntities())
            {
                user = db.Users.Single(u => u.UserId.Equals(user.UserId));

                List<Role> roles = user.Roles.Where(r => r.RoleName.Equals("Admin")).ToList();
                return roles.Any();
            }
        }

        public static bool IsLoggedin()
        {
            return (User)HttpContext.Current.Session["User"] != null && HttpContext.Current.Request.IsAuthenticated;
        }

        public static User CurrentUser()
        {
            return (User)HttpContext.Current.Session["User"] == null
                       ? null
                       : (User)HttpContext.Current.Session["User"];
        }

    }
}